import Vue from 'vue'
import Router from 'vue-router'
// import Login from '../components/login/login.vue'
// @指的是src下 相当于 ./src
import Login from '@/components/login/login.vue'
import Home from '@/components/home/home.vue'
import Users from '@/components/users/users.vue'
import Rights from '@/components/rights/rights.vue'
import Role from '@/components/role/role.vue'
import goodsList from '@/components/goods/goodslist.vue'
import goodsAdd from '@/components/goods/goodsadd.vue'
import Params from '@/components/goods/params.vue'
import Cate from '@/components/goods/categories.vue'
import Order from '@/components/orders/orders.vue'
import Reports from '@/components/reports/reports.vue'


Vue.use(Router)

const router = new Router({
  routes: [{
    name: 'login',
    path: '/login',
    component: Login
  },
  {
    name: 'home',
    path: '/',
    redirect:'/users',
    component: Home,
    children: [{
      name: 'users',
      path: '/users',
      component: Users
    },
    {
      name:'rights',
      path:'/rights',
      component:Rights
    },
    {
      name:'roles',
      path:'/roles',
      component:Role
    },
    {
      name:'goods',
      path:'/goods',
      component:goodsList
    },
    {
      name:'goodsadd',
      path:'/goodsadd',
      component:goodsAdd
    },
    {
      name:'params',
      path:'/params',
      component:Params
    },
    {
      name:'categories',
      path:'/categories',
      component:Cate
    },
    {
      name:'orders',
      path:'/orders',
      component:Order
    },
    {
      name:'reports',
      path:'/reports',
      component:Reports
    }

    ]
  }
  ]
})


router.beforeEach((to, from, next) => {
  // 如果要去的是login next()
  if(to.path ==='/login'){
    next()
  }else{
    // 如果去的不是login
    // 判断token token存在next()
    var token = localStorage.getItem('token')
    if(!token){
      
      router.push({name:'login'})
      return
    }
    next()
  }
})

export default router
